#ifndef BEATS_H
#define BEATS_H
#include "ecg.h"
#include <QByteArray>
#include <QDataStream>
#include <QDebug>
#include <QFile>
#include <QHash>
#include <QObject>
#include <QPoint>
#include <QPolygon>
#include <QVariantList>
#include <QVector>
#include <QtMath>

class Beats : public QObject {
    Q_OBJECT
private:
    explicit Beats(QObject* parent = nullptr);
    Ecg& ecg = Ecg::getInstance();
    bool isInPolygon(QPoint& point, QList<QPoint>& points);
    int selectedBeatsCount;

    Q_PROPERTY(int selectedBeatsCount READ getSelectedBeatsCount WRITE setSelectedBeatsCount NOTIFY selectedBeatsCountChanged FINAL)

public:
    static Beats& getInstance();
    // 删除拷贝构造函数和赋值运算符，防止拷贝实例
    Beats(const Beats&) = delete;
    Beats& operator=(const Beats&) = delete;
    enum dataIndexEnum {
        labelIndex,
        rrIntervalsIndex,
        templateIndex,
        selectedIndex,
        pixelXIndex, //散点图中X轴位置
        pixelYIndex, //散点图中Y轴位置
        pIndex,
        qIndex,
        sIndex,
        tIndex,
        test1Index = 100,
        test2Index,
        test3Index,
        test4Index,
        test5Index,
        test6Index,
        test7Index,
        test8Index,
        test9Index,
        test10Index,

    };
    enum labelsEnum { INVALID,
        N,
        A,
        S,
        V,
        J,
        G,
        P,
        B,
        F,
        E,
        R,
        W,
        D,
        Z,
        Y,
        T,
        X,
        O };
    QVector<QHash<QString, QString>> labels = {
        { { "label", "-" }, { "desc", "无效的" } },
        { { "label", "N" }, { "desc", "正常" } },
        { { "label", "A" }, { "desc", "房颤" } },
        { { "label", "S" }, { "desc", "房性早搏" } },
        { { "label", "V" }, { "desc", "室性早搏" } },
        { { "label", "J" }, { "desc", "交界性早搏" } },
        { { "label", "G" }, { "desc", "交界性逸博" } },
        { { "label", "P" }, { "desc", "起搏" } },
        { { "label", "B" }, { "desc", "束支传导阻滞" } },
        { { "label", "F" }, { "desc", "融合波" } },
        { { "label", "E" }, { "desc", "室性逸博" } },
        { { "label", "R" }, { "desc", "室内差异性传导" } },
        { { "label", "W" }, { "desc", "房性逸博" } },
        { { "label", "D" }, { "desc", "房早未下传" } },
        { { "label", "Z" }, { "desc", "房早伴室内差异性传导" } },
        { { "label", "Y" }, { "desc", "P波" } },
        { { "label", "T" }, { "desc", "T波" } },
        { { "label", "X" }, { "desc", "伪差" } },
        { { "label", "O" }, { "desc", "其他" } },
    };
    QHash<int, QHash<unsigned char, int16_t>> data;
    QVector<int> rPeaksInAlls; //由于QHash是无序的，所以将所有rPeaksInAlls保存排序
    QHash<int, bool> selectedBeats; //记录被选中的心搏
    QList<int> listBeats; //需要被列表显示的心搏
    QList<int> circleSelectBeats; //圈选的需要显示的心搏
    Q_INVOKABLE QVector<int> setSelectedBeats(QVector<int> rPeaksInAlls); //设置被选中的心搏
    void saveQHashToBinaryFile(QHash<int, QHash<unsigned char, int16_t>>& hash);
    void loadQHashFromBinaryFile();
    void processBeatsData();
    Q_INVOKABLE QVector<int> getFragmentBeats(int start, int duration);
    Q_INVOKABLE QVector<int> shortcutSetLabel(QString keyStr);
    Q_INVOKABLE QList<int> getRrIntervalChartData(int start, int duration, int width, int height);
    Q_INVOKABLE QList<int> getRrScatterChartData(int start, int duration, int width, int height, int axisMaxValue, QString label1, QString label2);
    Q_INVOKABLE int circleSelectRrScatterChartData(const QVariantList& points);
    int getSelectedBeatsCount() const;
    void setSelectedBeatsCount(int newSelectedBeatsCount);
signals:
    void selectedBeatsCountChanged();
};

#endif // BEATS_H
